package com.cloud.mall.gateway.controller;

import com.cloud.mall.gateway.common.BaseResponse;
import com.cloud.mall.gateway.common.Constants;
import com.cloud.mall.gateway.enums.CredentialType;
import com.cloud.mall.gateway.feign.Oauth2Client;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@Api(tags = "WEB登入模块")
public class WebLoginController {
    @Autowired
    private Oauth2Client oauth2Client;

    @ApiOperation("登陆")
    @PostMapping("/api/login")
    public BaseResponse login(String username, String password) {
        Map<String, String> parameters = new HashMap<>();
        parameters.put("grant_type", "password");
        parameters.put("client_id", "web");
        parameters.put("client_secret", "web");
        parameters.put("scope","app");
        parameters.put("username", username+ "|" + CredentialType.WEB.name());
        parameters.put("password", password);
        Map<String, Object> tokenInfo = oauth2Client.postAccessToken(parameters);
        return BaseResponse.setResultSuccess(tokenInfo);
    }

    /**
     * 系统刷新refresh_token
     *
     * @param refresh_token
     * @return
     */
    @ApiOperation("刷新")
    @PostMapping("/api/refresh_token")
    public Map<String, Object> refresh_token(String refresh_token) {
        Map<String, String> parameters = new HashMap<>();
        parameters.put(Constants.GRANT_TYPE, "refresh_token");
        parameters.put(Constants.CLIENT_ID, "web");
        parameters.put("client_secret", "web");
        parameters.put(Constants.SCOPE, "app");
        parameters.put("refresh_token", refresh_token);
        return oauth2Client.postAccessToken(parameters);
    }
}
